package com.zjw.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.zjw.annotation.domain.vo.AllCategoryListVo;
import com.zjw.annotation.domain.vo.AllValidCategoryListVo;
import com.zjw.annotation.domain.vo.CategoryListVo;
import com.zjw.annotation.domain.vo.PageVo;
import com.zjw.enums.AppHttpCodeEnum;
import com.zjw.mapper.ArticleMapper;
import com.zjw.constants.SystemConstants;
import com.zjw.annotation.domain.ResponseResult;
import com.zjw.annotation.domain.entity.Article;
import com.zjw.annotation.domain.entity.Category;
import com.zjw.mapper.CategoryMapper;
import com.zjw.service.CategoryService;
import com.zjw.utils.BeanCopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 分类表(Category)表服务实现类
 *
 * @author makejava
 * @since 2023-12-12 13:57:37
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {


    @Autowired
    private ArticleMapper articleMapper;

    @Override
    public ResponseResult getCategoryList() {
//        先获取所有有正式发布文章
        LambdaQueryWrapper<Article> tempWrapper = new LambdaQueryWrapper<>();
        tempWrapper.eq(Article::getStatus, SystemConstants.ARTICLE_STATUS_NORMAL);
        List<Article> articles = articleMapper.selectList(tempWrapper);

//        将所有有效的分类id放在集合中去重
        Set<Long> idSet = articles.stream()
                .map(a -> a.getCategoryId()).collect(Collectors.toSet());

//        创建wrapper
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(Category::getId, idSet);
        wrapper.eq(Category::getStatus, SystemConstants.CATEGORY_STATUS_NORMAL);

//        封装VO
        List res = BeanCopyUtils.copyList(list(wrapper), CategoryListVo.class);
        return ResponseResult.okResult(res);
    }

    /**
     * 获取所有类别列表
     *
     * @return {@link ResponseResult}
     */
    @Override
    public ResponseResult getValidCategoryList() {
        QueryWrapper<Category> wrapper = new QueryWrapper<>();
        wrapper.eq("status", SystemConstants.CATEGORY_STATUS_NORMAL);
        List<Category> list = list(wrapper);
        List<AllValidCategoryListVo> res = BeanCopyUtils.copyList(list, AllValidCategoryListVo.class);
        return ResponseResult.okResult(res);
    }

    @Override
    public ResponseResult getAllCategoryList(Long pageNum, Long pageSize) {
        IPage<Category> page = new Page<>(pageNum, pageSize);
        page(page);
        List<Category> raw = page.getRecords();
        List<AllCategoryListVo> resList = BeanCopyUtils.copyList(raw, AllCategoryListVo.class);

        PageVo res = new PageVo();
        res.setRows(resList);
        res.setTotal(page.getTotal());

        return ResponseResult.okResult(res);
    }

    /**
     * 分类名是否已经存在
     * @param name
     * @return {@link Boolean}
     */
    private Boolean categoryIsExist(String name, Long id){

        QueryWrapper<Category> wrapper = new QueryWrapper<>();
        wrapper.eq("name", name);
        wrapper.ne("id", id);
        if(!list(wrapper).isEmpty()){
//            非空证明已经存在
            return true;
        }
        return false;
    }

    @Override
    public ResponseResult addCategory(Category category) {
        if(categoryIsExist(category.getName(), category.getId())){
//            已经存在
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_INVALID_CHECK);
        }

        save(category);
        return ResponseResult.okResult();
    }


    @Override
    public ResponseResult getCategory(Long id) {
        Category c = getById(id);
        return ResponseResult.okResult(BeanCopyUtils.copy(c, AllCategoryListVo.class));
    }

    @Override
    public ResponseResult saveCategery(Category category) {
        if(categoryIsExist(category.getName(), category.getId())){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_INVALID_CHECK);
        }
        updateById(category);
        return ResponseResult.okResult();
    }

    /**
     * 批量删除分类
     *
     * @param idList ID 列表
     * @return {@link ResponseResult}
     */
    @Override
    public ResponseResult delCategories(List<Long> idList) {
        for (Long id : idList) {
            removeById(id);
        }
        return ResponseResult.okResult();
    }
}

